﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using LowLevelGraphics.Types.Interval;

namespace LowLevelGraphics.Filter
{
    public enum MASK
    {
        NONE,
        AND,
        OR
    }

    public class LocateColor : BaseForeBackgroundColorFilter
    {
        protected Color m_ColorBegin = Color.White;
        protected Color m_ColorEnd = Color.Black;
        protected MASK m_bMask = MASK.NONE;

        /// <summary>
        /// 
        /// </summary>
        public LocateColor()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        public LocateColor(Color _clBegin, Color _clEnd)
        {
            m_ColorBegin = _clBegin;
            m_ColorEnd = _clEnd;
        }

        /// <summary>
        /// used to clone this instance
        /// </summary>
        /// <param name="_locateColor"></param>
        internal LocateColor(LocateColor _locateColor)
        {
            m_ColorBegin = _locateColor.m_ColorBegin;
            m_ColorEnd = _locateColor.m_ColorEnd;
            m_bMask = _locateColor.m_bMask;
        }

        /// <summary>
        /// 
        /// </summary>
        public MASK MASK
        {
            get { return m_bMask; }
            set { m_bMask = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public Color BeginColor
        {
            get { return m_ColorBegin; }
            set { m_ColorBegin = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public Color EndColor
        {
            get { return m_ColorEnd; }
            set { m_ColorEnd = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            int nWidth = _bitmap.Width;
            int nHeight = _bitmap.Height;
            
            int nRB = m_ColorBegin.R;
            int nGB = m_ColorBegin.G;
            int nBB = m_ColorBegin.B;

            int nRE = m_ColorEnd.R;
            int nGE = m_ColorEnd.G;
            int nBE = m_ColorEnd.B;

            int y = 0;
            int x = 0;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    Color color = _bitmap.GetPixel(x, y);
                    int nR = color.R;
                    int nG = color.G;
                    int nB = color.B;
                    if (!(nR > nRB && nR < nRE
                        && nG > nGB && nG < nGB
                        && nB > nBE && nB < nBE))
                    {
                        _bitmap.SetPixel(x, y, m_ColorBackground);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Clones this instance
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new LocateColor(this);
        }
    }
}
